home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / memory / mtlxms21 / xmstest.cpp < prev   
Encoding:
C/C++ Source or Header  |  1994-09-14  |  11.3 KB  |  398 lines

  1. //--------------------------------------------------------------------------
  2. //
  3. //      XMSTEST.CPP: Test program for XMS class.
  4. //      Copyright (c) J.English 1993.
  5. //      Author's address: je@unix.brighton.ac.uk
  6. //
  7. //      Permission is granted to use copy and distribute the
  8. //      information contained in this file provided that this
  9. //      copyright notice is retained intact and that any software
  10. //      or other document incorporating this file or parts thereof
  11. //      makes the source code for the library of which this file
  12. //      is a part freely available.
  13. //
  14. //    MODIFIED BY MAXTAL P/L. Added more exhaustive XMS IO test:
  15. //    option 6 in the menu.
  16. //--------------------------------------------------------------------------
  17. //
  18. //      Revision history:
  19. //      2.0     Nov 1993        Initial coding
  20. //    2.1    Aug 1994    Added random XMS IO test
  21. //--------------------------------------------------------------------------
  22.  
  23. #include "xms.h"
  24. #include "doserror.h"
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <time.h>
  28. #include <string.h>
  29.  
  30. //--------------------------------------------------------------------------
  31. //
  32. //      Global data.
  33. //
  34. DOSerror e;             // guard against critical errors and control-breaks
  35. XMS* desc [100];        // array of pointers to XMS descriptors
  36. int ndesc = 0;          // number of descriptors
  37.  
  38.  
  39. //--------------------------------------------------------------------------
  40. //
  41. //      Function prototypes.
  42. //
  43. char menu ();           // display menu & get user's choice
  44. void XMSstats ();       // 1. XMS statistics
  45. void blocksizes ();     // 2. List block sizes
  46. void allocate ();       // 3. Allocate block
  47. void deallocate ();     // 4. Deallocate block
  48. void copy ();           // 5. Copy block
  49. void randcopy ();       // 6. Copy block
  50. void resize ();         // 7. Resize block
  51.  
  52. //--------------------------------------------------------------------------
  53. //
  54. //      Main program.
  55. //
  56. //      A menu of choices is displayed, allowing the user to exercise
  57. //      each of the XMS functions available (with the exception of
  58. //      XMS-to-XMS copying).
  59. //
  60. void main ()
  61. {
  62.     XMSstats();
  63.  
  64.     char choice;
  65.     for (;;)
  66.     {
  67.         choice = menu ();
  68.         if (choice == 'X')
  69.             break;
  70.  
  71.         switch (choice)
  72.         {
  73.             case '1':
  74.                 XMSstats ();
  75.                 break;
  76.             case '2':
  77.                 blocksizes ();
  78.                 break;
  79.             case '3':
  80.                 allocate ();
  81.                 break;
  82.             case '4':
  83.                 deallocate ();
  84.                 break;
  85.             case '5':
  86.                 copy ();
  87.                 break;
  88.             case '6':
  89.                 randcopy ();
  90.                 break;
  91.             case '7':
  92.                 resize ();
  93.                 break;
  94.         }
  95.     }
  96.     for (int i = 0; i < ndesc; i++)
  97.         delete desc[i];
  98. }
  99.  
  100. //--------------------------------------------------------------------------
  101. //
  102. //      Display menu and get user choice.
  103. //
  104. //      The choice must be a line containing a single character from 1 to 6
  105. //      (or X for exit).
  106. //
  107. char menu ()
  108. {
  109.     printf ("\nChoose desired operation:\n"
  110.             "  1) XMS statistics\n"
  111.             "  2) List block sizes\n"
  112.             "  3) Allocate block\n"
  113.             "  4) Deallocate block\n"
  114.             "  5) Copy block\n"
  115.             "  6) Random Write test\n"
  116.             "  7) Resize block\n"
  117.             "  X) Exit program\n");
  118.     char buff [80];
  119.     for (;;)
  120.     {
  121.         printf ("Enter your choice: ");
  122.         fgets (buff, 80, stdin);
  123.         if ((buff[0] == 'x' || buff[0] == 'X') && buff[1] == '\n')
  124.             return 'X';
  125.         if (buff[0] < '1' || buff[0] > '6' || buff[1] != '\n')
  126.             printf ("Invalid choice!\a\n");
  127.         else
  128.             break;
  129.     }
  130.     printf ("\n");
  131.     return buff[0];
  132. }
  133.  
  134.  
  135. //--------------------------------------------------------------------------
  136. //
  137. //      Display XMS statistics.
  138. //
  139. void XMSstats ()
  140. {
  141.     printf ("XMS available: %ld bytes\nLargest block: %ld bytes\n",
  142.             XMS::available(), XMS::largest());
  143. }
  144.  
  145.  
  146. //--------------------------------------------------------------------------
  147. //
  148. //      List block sizes.
  149. //
  150. void blocksizes ()
  151. {
  152.     int flag = 0;
  153.     for (int i = 0; i < ndesc; i++)
  154.     {   if (desc[i] != 0)
  155.         {   printf ("Block %d: size = %ld\n", i+1, desc[i]->size());
  156.             flag = 1;
  157.         }
  158.     }
  159.     if (flag == 0)
  160.         printf ("No blocks allocated!\n");
  161. }
  162.  
  163. //--------------------------------------------------------------------------
  164. //
  165. //      Allocate block.
  166. //
  167. //      Attempt to allocate a new block of a specified size and report the
  168. //      result.
  169. //
  170. void allocate ()
  171. {
  172.     printf ("Enter block size: ");
  173.     long size;
  174.     scanf ("%ld", &size);
  175.     while (getchar() != '\n')
  176.         continue;
  177.     desc [ndesc++] = new XMS (size);
  178.     printf ("Block %d: ", ndesc);
  179.     printf ("requested %ld bytes, granted %ld bytes.\n",
  180.             size, desc[ndesc-1]->size());
  181.     if (!desc[ndesc-1]->valid())
  182.         delete desc[--ndesc];
  183. }
  184.  
  185.  
  186. //--------------------------------------------------------------------------
  187. //
  188. //      Deallocate block.
  189. //
  190. //      Attempt to deallocate an existing block and report the result.
  191. //
  192. void deallocate ()
  193. {
  194.     printf ("Enter block number: ");
  195.     int b;
  196.     scanf ("%d", &b); b--;
  197.     while (getchar() != '\n')
  198.         continue;
  199.     if (b < 0 || b >= ndesc || desc[b] == 0)
  200.         printf ("No such block!\n");
  201.     else
  202.     {   delete desc[b];
  203.         desc[b] = 0;
  204.         printf ("Block %d deallocated.\n", b+1);
  205.     }
  206. }
  207.  
  208. //--------------------------------------------------------------------------
  209. //
  210. //      Copy to/from XMS.
  211. //
  212. //      Prompts for a block number and a transfer size.  An array of random
  213. //      numbers of the specified size is created in conventional memory,
  214. //      copied to the specified block starting at the specified offset,
  215. //      copied back to conventional memory, and finally compared with the
  216. //      original block.
  217. //
  218. void copy ()
  219. {
  220.     printf ("Enter block number:  ");
  221.     int b;
  222.     scanf ("%d", &b); b--;
  223.     while (getchar() != '\n')
  224.         continue;
  225.     if (b < 0 || b >= ndesc || desc[b] == 0)
  226.     {   printf ("No such block!\n");
  227.         return;
  228.     }
  229.  
  230.     printf ("Enter transfer size: ");
  231.     unsigned s;
  232.     scanf ("%u", &s);
  233.     while (getchar() != '\n')
  234.         continue;
  235.     char* x = new char [s];
  236.     char* y = new char [s];
  237.     if (x == 0 || y == 0)
  238.     {   printf ("Not enough real memory!\n");
  239.         return;
  240.     }
  241.  
  242.     printf ("Enter block offset:  ");
  243.     long p;
  244.     scanf ("%ld", &p);
  245.     while (getchar() != '\n')
  246.         continue;
  247.     printf ("Copying %u bytes to/from XMS block %d, offset %ld.\n", s, b, p);
  248.     printf ("Generating random data... "); fflush (stdout);
  249.     randomize ();
  250.     for (unsigned i = 0; i < s; i++)
  251.         x[i] = random (256);
  252.     printf ("done.\n");
  253.  
  254.     XMS::error r = XMS::copy (desc[b]->at(p), x, s);
  255.     printf ("Random data copied to XMS, result code %02X\n", int(r));
  256.     if (r == XMS::SUCCESS)
  257.     {
  258.         // MODIFIED BY MAXTAL TO SCRAMBLE BEFORE CHECKING!
  259.         for (unsigned i = 0; i < s; i++)
  260.           y[i] = random (256);
  261.  
  262.         r = XMS::copy (y, desc[b]->at(p), s);
  263.         printf ("Data copied back from XMS, result code %02X\n", int(r));
  264.         if (r == XMS::SUCCESS)
  265.         {   for (i = 0; i < s; i++)
  266.             {   if (x[i] != y[i])
  267.                 {   printf ("Verification error at offset %u.\n", i);
  268.                     break;
  269.                 }
  270.             }
  271.             if (i == s)
  272.                 printf ("Copy verified successfully.\n");
  273.         }
  274.     }
  275.     delete x;
  276.     delete y;
  277. }
  278.  
  279. //--------------------------------------------------------------------------
  280. //
  281. //      RANDOM Copy to/from XMS.
  282. //
  283. void randcopy ()
  284. {
  285.     printf ("Enter block size:  ");
  286.     int blksize;
  287.     scanf ("%d", &blksize);
  288.     while (getchar() != '\n')
  289.         continue;
  290.  
  291.     printf ("Enter number of iterations size:  ");
  292.     int iterations;
  293.     scanf ("%d", &iterations);
  294.     while (getchar() != '\n')
  295.         continue;
  296.  
  297.     XMS *xmsBlock = new XMS(blksize); // blksize byte block
  298.     XMS *xmsBlock2 = new XMS(blksize); // blksize byte block
  299.     unsigned char* x = (char*)malloc(blksize);
  300.     unsigned char* y = (char*)malloc(blksize);
  301.     if (x == 0 || y == 0)
  302.     {   printf ("Not enough real memory!\n");
  303.         return;
  304.     }
  305.  
  306.     for (int i = 0; i < blksize; i++)
  307.     {
  308.         y[i] = x[i] = random (256);
  309.     }
  310.     XMS::copy (xmsBlock->at(0), x, blksize);
  311.     XMS::copy (xmsBlock2->at(0), x, blksize);
  312.  
  313.     for(int j = 0; j<iterations; ++j)
  314.     {
  315.         unsigned long start = random(blksize);
  316.         unsigned long len   = random(blksize-start);
  317.         printf ("%lu bytes XMS offset %ld ... ", len, start);
  318.  
  319.         for (unsigned i = start; i < start+len; i++) x[i] = random (256);
  320.         XMS::copy (xmsBlock->at(start), x+start, len);
  321.         XMS::copy (xmsBlock2->at(start), xmsBlock->at(start), len);
  322.  
  323.         memset(y,0,blksize);
  324.         XMS::copy (y+start, xmsBlock->at(start), len);
  325.         for (i = 0; i < start; i++)
  326.             {   if (0 != y[i])
  327.                 {   printf ("Verification error 1 at byte %u.\n", i);
  328.                     return;
  329.                 }
  330.             }
  331.         for (i = start; i < start+len; i++)
  332.             {   if (x[i] != y[i])
  333.                 {   printf ("Verification error 2 at byte %u.\n", i);
  334.                     return;
  335.                 }
  336.             }
  337.         for (i = start+len; i < blksize; i++)
  338.             {   if (0 != y[i])
  339.                 {   printf ("Verification error 3 at byte %u.\n", i);
  340.                     return;
  341.                 }
  342.             }
  343.  
  344.         memset(y,0,blksize);
  345.         XMS::copy (y, xmsBlock->at(0), blksize);
  346.         for (i = 0; i < blksize; i++)
  347.             {   if (x[i] != y[i])
  348.                 {   printf ("Verification error 4 at byte %u.\n", i);
  349.                     return;
  350.                 }
  351.             }
  352.  
  353.         memset(y,0,blksize);
  354.         XMS::copy (y, xmsBlock2->at(0), blksize);
  355.         for (i = 0; i < blksize; i++)
  356.             {   if (x[i] != y[i])
  357.                 {   printf ("Verification error 5 at byte %u.\n", i);
  358.                     return;
  359.                 }
  360.             }
  361.         printf("\n");
  362.     }
  363.     free(x);
  364.     free(y);
  365.     delete xmsBlock;
  366.     delete xmsBlock2;
  367. }
  368.  
  369. //--------------------------------------------------------------------------
  370. //
  371. //      Resize an existing block.
  372. //
  373. //      Attempt to resize an existing XMS allocation and report the result.
  374. //
  375. void resize ()
  376. {
  377.     printf ("Enter block number: ");
  378.     int b;
  379.     scanf ("%d", &b); b--;
  380.     while (getchar() != '\n')
  381.         continue;
  382.     if (b < 0 || b >= ndesc || desc[b] == 0)
  383.     {   printf ("No such block!\n");
  384.         return;
  385.     }
  386.  
  387.     printf ("Current size:   %ld\nEnter new size: ", desc[b]->size());
  388.     long size;
  389.     scanf ("%ld", &size);
  390.     while (getchar() != '\n')
  391.         continue;
  392.     XMS::error r = desc[b]->resize (size);
  393.     if (r == XMS::SUCCESS)
  394.         printf ("Requested %ld, granted %ld.\n", size, desc[b]->size());
  395.     else
  396.         printf ("Resize failed: error code = %02X\n", int(r));
  397. }
  398.